package com.thoughtmechanix.licenses.services;

import com.thoughtmechanix.licenses.OrganizationDiscoveryClient;
import com.thoughtmechanix.licenses.config.ServiceConfig;
import com.thoughtmechanix.licenses.model.License;
import com.thoughtmechanix.licenses.model.Organization;
import com.thoughtmechanix.licenses.repository.LicenseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 功能描述
 *
 * @author zgl
 * @version 1.0
 * @date 2019-07-26 17:40
 */

@Service
public class LicenseService {

    @Autowired
    private LicenseRepository repository;

    @Autowired
    private ServiceConfig config;
    @Autowired
    private OrganizationDiscoveryClient discoveryClient;

    public License getLicense(String orgId, String licenseId){
       License license =  repository.findByOrAndOrganizationIdAndId(orgId,licenseId);
       return  license.withComment(config.getExampleProperty());
    }

    public List<License> getLicenseByOrgId(String  orgId){
        return repository.findByOrAndOrganizationId(orgId);

    }

    public void saveLicense(License license){
        license.withId(UUID.randomUUID().toString());
        repository.save(license);
    }

    public License getLicense(String organizationId, String licenseId, String clientType){
        License license = repository.findByOrAndOrganizationIdAndId(organizationId, licenseId);
        Organization org = retrieveOrgInfo(organizationId);
        return  license.withOrganizationId( org.getId());

        
    }

    private Organization retrieveOrgInfo(String organizationId) {
       return  discoveryClient.getOrganization(organizationId);
    }


}
